package com.cxyd.modules.wms.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.json.JSONArray;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.PurchaseDTO;
import com.cxyd.modules.wms.excel.ImportStockExcel;
import com.cxyd.modules.wms.excel.PurchaseDetailExcel;
import com.cxyd.modules.wms.service.PurchaseService;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


/**
 * 入库主单
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Slf4j
@RestController
@RequestMapping("wms/purchase")
public class PurchaseController {
    private final PurchaseService purchaseService;

    public PurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:purchase:page")
    public Result<PageData<PurchaseDTO>> page(@RequestParam Map<String, Object> params) {
        params.put("purchaseType", "采购入库");
        PageData<PurchaseDTO> page = purchaseService.page(params);

        return new Result<PageData<PurchaseDTO>>().ok(page);
    }

    @GetMapping("cpPage")
    @RequiresPermissions("wms:purchase:cpPage")
    public Result<PageData<PurchaseDTO>> cpPage(@RequestParam Map<String, Object> params) {
        params.put("purchaseType", "成品入库");
        PageData<PurchaseDTO> page = purchaseService.page(params);

        return new Result<PageData<PurchaseDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:purchase:info")
    public Result<PurchaseDTO> get(@PathVariable("id") Long id) {
        PurchaseDTO data = purchaseService.get(id);

        return new Result<PurchaseDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:purchase:save")
    public Result save(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        purchaseService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:purchase:update")
    public Result update(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        purchaseService.update(dto);

        return new Result();
    }


    @DeleteMapping
    @LogOperation("删除")
//    @RequiresPermissions("wms:purchase:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "purchaseId");

        purchaseService.delete(ids);

        return new Result();
    }

    @PostMapping("/upload")
    @LogOperation("原料入库上传")
    @RequiresPermissions("wms:purchase:upload")
    public Result upload(MultipartFile file) {
        try {
            purchaseService.uploadFile(file);
        } catch (Exception e) {
            return new Result().error("导入失败，请重试！" + e.getMessage());
        }
        return new Result().ok("导入成功！");
    }

    @PostMapping("/uploadCP")
    @LogOperation("成品入库上传")
    @RequiresPermissions("wms:purchase:uploadCP")
    public Result uploadCP(MultipartFile file) {
        try {
            purchaseService.uploadCPFile(file);
        } catch (Exception e) {
            return new Result().error("导入失败，请重试！" + e.getMessage());
        }
        return new Result().ok("导入成功！");
    }
}